package com.army.study.utils;

import com.army.study.ResourceTable;
import com.army.study.common.DateUtils;
import com.army.study.database.Form;
import ohos.agp.components.ComponentProvider;
import ohos.agp.utils.Color;
import ohos.app.Context;

import java.util.Calendar;

public class ComponentProviderUtils {
    // 当前星期颜色
    private static Color nowWeekColor = new Color(Color.rgb(255, 245, 238));
    // 原色星期
    private static Color primaryWeekColor = new Color(Color.rgb(192, 192, 192));

    private static final int WEEK_DAYS = 7;         // 一个星期七天
    private static final int STRING_LENGTH = 2;     // 字符串长度
    private static final int DIM_VERSION = 3;       // 暗淡版本
    private static final int SUNDAY = 1;            // 星期天
    private static final int MONDAY = 2;            // 星期一
    private static final int TUESDAY = 3;           // 星期二
    private static final int WEDNESDAY = 4;         // 星期三
    private static final int THURSDAY = 5;          // 星期四
    private static final int FRIDAY = 6;            // 星期五
    private static final int SATURDAY = 7;          // 星期六

    /**
     * 获取星期几对应的组件ID值
     * @return
     */
    public static int getWeekDayId() {
        Calendar calendar = Calendar.getInstance();
        int week = calendar.get(Calendar.DAY_OF_WEEK);
        int result = getWeekIdResult(week);
        return result;
    }

    /**
     * 获取周组件id
     * @param week
     * @return
     */
    private static int getWeekIdResult(int week) {
        int result = ResourceTable.Id_mon;
        switch (week) {
            case SUNDAY:
                result = ResourceTable.Id_sun;
                break;
            case MONDAY:
                result = ResourceTable.Id_mon;
                break;
            case TUESDAY:
                result = ResourceTable.Id_tue;
                break;
            case WEDNESDAY:
                result = ResourceTable.Id_wed;
                break;
            case THURSDAY:
                result = ResourceTable.Id_thu;
                break;
            case FRIDAY:
                result = ResourceTable.Id_fri;
                break;
            case SATURDAY:
                result = ResourceTable.Id_sat;
                break;
            default:
                result = ResourceTable.Id_sun;
                break;
        }
        return result;
    }

    public static ComponentProvider getComponentProvider(Form form, Context context) {
        int layoutId = ResourceTable.Layout_form_image_with_info_date_card_2_2;
        // 判断卡片规格是否是3 然后赋值不同的布局ID
        if (form.getDimension() == DIM_VERSION) {
            layoutId = ResourceTable.Layout_form_image_with_info_date_card_2_4;
        }

        ComponentProvider componentProvider = new ComponentProvider(layoutId, context);
        setComponentProviderValue(componentProvider);
        return componentProvider;
    }

    /**
     * 设置componentProvider的值
     * @param componentProvider
     */
    private static void setComponentProviderValue(ComponentProvider componentProvider) {
        Calendar now = Calendar.getInstance();
        int hour = now.get(Calendar.HOUR_OF_DAY);
        int min = now.get(Calendar.MINUTE);
        int second = now.get(Calendar.SECOND);

        String hourString = int2String(hour);
        String minString = int2String(min);
        String secondString = int2String(second);

        componentProvider.setText(ResourceTable.Id_date, DateUtils.getCurrentDate("yyyy-MM-dd"));
        componentProvider.setText(ResourceTable.Id_hour, hourString);
        componentProvider.setText(ResourceTable.Id_min, minString);
        componentProvider.setText(ResourceTable.Id_sec, secondString);

        // 获取当前星期
        int weekDayId = getWeekDayId();
        componentProvider.setTextColor(weekDayId, nowWeekColor);

        // 将前一天的星期改回原色
        int lastWeekId = getLastWeekDayId();
        componentProvider.setTextColor(lastWeekId, primaryWeekColor);
    }

    /**
     * 转换为字符串的时间
     * @param time
     * @return
     */
    private static String int2String(int time) {
        String timeString;
        // 判断当前时间是否两位数
        if (String.valueOf(time).length() < STRING_LENGTH) {
            timeString = "0" + time;
        } else {
            timeString = time + "";
        }
        return timeString;
    }

    /**
     * 获取一周的前一天
     * @return
     */
    public static int getLastWeekDayId() {
        Calendar calendar = Calendar.getInstance();
        int week = calendar.get(Calendar.DAY_OF_WEEK);
        int lastWeek;
        if (week == 1) {
            lastWeek = WEEK_DAYS;
        } else {
            lastWeek = week - 1;
        }
        return getWeekIdResult(lastWeek);
    }
}
